package IO;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @className: Demo14
 * @author: 芃
 * @description: 对象的写入和加载
 * @date: 2024/6/26 23:37
 */
class Person{
    private String name;
    private int age;
    private String id;

    public Person() {
    }

    public Person(String name, int age, String id) {
        this.name = name;
        this.age = age;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", id='" + id + '\'' +
                '}';
    }
}
public class Demo14 {
    // 对象写入->使用Writer写入文件
    public void save(String filePath,List<Person> persons) throws IOException {
        Writer writer = new FileWriter(filePath);
        for (Person person : persons) {
            writer.write(person.getName() + "," + person.getAge() + "," + person.getId() + "\n");
        }
        writer.close();
    }
    // 对象写入2->使用BufferedWriter写入文件
    public void save2(String filePath,List<Person> persons) throws IOException {
        Path path = Paths.get(filePath);
        BufferedWriter writer = Files.newBufferedWriter(path);
        for (Person person : persons) {
            //写入方式1
            writer.write(person.getName() + "," + person.getAge() + "," + person.getId() + "\n");
            //写入方式2
            //writer.write(person.getName() + "," + person.getAge() + "," + person.getId());
            writer.newLine();
        }
        writer.close();
    }

    //对象写入3-》使用OutputStream写入文件
    public void save3(String filePath,List<Person> persons) throws IOException {
        try(OutputStream outputStream = new FileOutputStream(filePath)){
            for (Person person : persons) {
                String p = person.getName() + "," + person.getAge() + "," + person.getId() + "\n";
                outputStream.write(p.getBytes());
            }
        }catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    // 对象加载->使用BufferedReader读取文件
    public List<Person> load(String filePath) throws IOException {
        List<Person> persons = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        String person = reader.readLine();
        while(person != null){
            Person p = new Person();
            String[] parts = person.split(",");
            p.setName(parts[0]);
            p.setAge(Integer.parseInt(parts[1]));
            p.setId(parts[2]);
            persons.add(p);
            person = reader.readLine();
        }
        reader.close();
        return persons;
    }


    public static void main(String[] args) throws IOException {
        Person person = new Person("张三", 20, "123456");
        Person person2 = new Person("李四",21,"654321");
        Person person3 = new Person("王五",18,"789456");
        List<Person> persons = new ArrayList<>();
        persons.add(person);
        persons.add(person2);
        persons.add(person3);
        Demo14 demo14 = new Demo14();
        demo14.save("D:\\My_Code\\Java_Code\\EE_And_System_New\\EE_TestDemo\\src\\IO\\Person.txt", persons);
        List<Person> persons2 = demo14.load("D:\\My_Code\\Java_Code\\EE_And_System_New\\EE_TestDemo\\src\\IO\\Person.txt");
        for (Person p : persons2) {
            //System.out.println(p.getName() + "," + p.getAge() + "," + p.getId());
        }
    }




    public static void main1(String[] args) throws IOException {
        Writer writer = new FileWriter("D:\\My_Code\\Java_Code\\EE_And_System_New\\EE_TestDemo\\src\\IO\\Person.txt");
        writer.write("helloworld");
        writer.close();

        try (InputStream inputStream = new FileInputStream("D:\\My_Code\\Java_Code\\EE_And_System_New\\EE_TestDemo\\src\\IO\\Person.txt")) {
            byte[] bytes = new byte[1024];
            int len = inputStream.read(bytes);
            for (int i = 0; i < len; i++) {
                System.out.printf("%c\n", bytes[i]);
            }
        }
    }
}
